﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMS;

namespace Test_MySimpleDBM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text += FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion;
            this.toolTip1.SetToolTip(this.linkLabel1, "反馈信息 邮件至lipeng_3g@hotmail.com");
            this.toolTip1.SetToolTip(this.pictureBox1, "反馈信息 邮件至lipeng_3g@hotmail.com");
        }
        /// <summary>
        /// 测试连接数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_test_Click(object sender, EventArgs e)
        {
            if (textBox_ip.Text.Length == 0 || textBox_name.Text.Length == 0 || textBox_pwd.Text.Length == 0)
            {
                MessageBox.Show("请先填写连接信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (SqlHelper.ApplyConnectionString(textBox_ip.Text, textBox_name.Text, textBox_pwd.Text, "mysql", true))
            {
                ConsoleOut("连接数据库成功，当前连接库为mysql", true);
                DataTable table = SqlHelper.Query("show databases");
                if (table.Rows.Count > 0)
                {
                    comboBox_dbs.Items.Clear();
                    foreach (DataRow item in table.Rows)
                    {
                        comboBox_dbs.Items.Add(item["Database"].ToString());
                    }
                    ConsoleOut("获取所有库，请选择想要打开的库，然后点击【打开数据库】按钮", true);
                }
                else
                {
                    ConsoleOut("未能获取到数据表！", false);
                }
            }
            else
            {
                MessageBox.Show("连接数据库失败！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ConsoleOut("【异常信息】" + SqlHelper.ExceptionString, false);
                return;
            }
        }

        private void button_opendb_Click(object sender, EventArgs e)
        {
            if (textBox_ip.Text.Length == 0 || textBox_name.Text.Length == 0 || textBox_pwd.Text.Length == 0)
            {
                MessageBox.Show("请先填写连接信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBox_dbs.SelectedItem == null)
            {
                MessageBox.Show("请选择要打开的数据库！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (SqlHelper.ApplyConnectionString(textBox_ip.Text, textBox_name.Text, textBox_pwd.Text, comboBox_dbs.SelectedItem.ToString(), true))
            {
                ConsoleOut("打开数据库" + comboBox_dbs.SelectedItem.ToString() + "成功！", true);
            }
            else
            {
                MessageBox.Show("连接数据库失败！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ConsoleOut("打开数据库" + comboBox_dbs.SelectedItem.ToString() + "失败！", false);
                ConsoleOut("【异常信息】" + SqlHelper.ExceptionString, false);
            }
        }

        public void ConsoleOut(string info, bool success)
        {
            info = DateTime.Now.ToString("HH:mm:ss") + ":" + info;
            int p1 = richTextBox_console.TextLength;  //取出未添加时的字符串长度。   
            richTextBox_console.AppendText(info + "\r\n");  //保留每行的所有颜色。 //  rtb.Text += strInput + "/n";  //添加时，仅当前行有颜色。   
            int p2 = info.Length;  //取出要添加的文本的长度   
            richTextBox_console.Select(p1, p2);        //选中要添加的文本   
            if (success)
            {
                richTextBox_console.SelectionColor = Color.Yellow;
            }
            else
            {
                richTextBox_console.SelectionColor = Color.OrangeRed;
            }
            richTextBox_console.ScrollToCaret();
        }

        private void button_submit_Click(object sender, EventArgs e)
        {
            if (radioButton_select.Checked)
            {
                dataGridView1.Visible = true;
                DataTable table = SqlHelper.Query(richTextBox_sql.Text);
                if (SqlHelper.QueryFlag)
                {
                    ConsoleOut("执行完成！", true);
                    ConsoleOut(richTextBox_sql.Text, true);
                    if (table.Rows.Count > 0)
                    {
                        dataGridView1.DataSource = table;
                    }
                    MessageBox.Show("执行完成", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ConsoleOut("执行失败！", false);
                    ConsoleOut("【异常信息】" + SqlHelper.ExceptionString, false);
                    ConsoleOut(richTextBox_sql.Text, false);
                    MessageBox.Show("执行失败", "信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                dataGridView1.Visible = false;
                if (SqlHelper.Update(richTextBox_sql.Text))
                {
                    ConsoleOut("执行成功！受影响的行数为 " + SqlHelper.AffectedRows, true);
                    ConsoleOut(richTextBox_sql.Text, true);
                    MessageBox.Show("执行成功！受影响的行数为 " + SqlHelper.AffectedRows, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ConsoleOut("执行失败！", false);
                    ConsoleOut("【异常信息】" + SqlHelper.ExceptionString, false);
                    ConsoleOut(richTextBox_sql.Text, false);
                    MessageBox.Show("执行失败", "信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:lipeng_3g@hotmail.com?Subject=[Feedback] MySql简单管理工具" + FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            linkLabel1_LinkClicked(null, null);
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button_test_Click(null, null);
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            this.groupBox2.Width = this.Width - 274;
            this.groupBox3.Width = this.Width - 40;
            this.groupBox3.Height = this.Height - 419;
            this.linkLabel1.Location = new System.Drawing.Point(this.Width - 167, this.Height - 56);
        }
    }
}
